package org.baicaixiaozhan.learncommon.config.gson;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
 * 美元符号反序列化
 *
 * @since 2021/4/13
 * @author baicaixiaozhan
 */
public final class DollarSignDeserializer extends AbstractCurrencyFormatDeserializer<BigDecimal> {

    public static final String DOLLAR = "$";
    public static final String EMPTY = "";

    @Override
    public BigDecimal deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        String str = json.getAsString();
        str = str.replace(DOLLAR, EMPTY);
        return BigDecimal.valueOf(Double.parseDouble(str));
    }
}
